﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using BUS;
using DTO;

namespace ourAdd_in
{
    public partial class ucRegister : UserControl
    {
        public ucRegister()
        {
            InitializeComponent();
        }

        private bool IsValidEmailAddress(string sEmail)
        {
            if (sEmail == null)
            {
                return false;
            }

            int nFirstAT = sEmail.IndexOf('@');
            int nLastAT = sEmail.LastIndexOf('@');

            if ((nFirstAT > 0) && (nLastAT == nFirstAT) &&
            (nFirstAT < (sEmail.Length - 1)))
            {
                // address is ok regarding the single @ sign
                return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
            }
            else
            {
                return false;
            }
        }
        
        private bool EmailExists(string email)
        {
            AccountBUS bus = new AccountBUS();
            return bus.CheckEmail(email.Trim());
        }

        private void txtEmail_Validated(object sender, EventArgs e)
        {
            if (txtEmail.Text.CompareTo("")==0)
            {
                btnCancel.Focus();
                return;
            }
            if (!IsValidEmailAddress(txtEmail.Text))
            {
                MessageBox.Show("Email sai định dạng");
                txtEmail.Focus();
            }
            else if (EmailExists(txtEmail.Text))
            {
                MessageBox.Show("Đã tồn tại Email này.", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtEmail.Focus();
            }
        }

        private void btnRegist_Click(object sender, EventArgs e)
        {
            // HASH PASS, LAY SALT VOI MD5
            string password = txtPassPhrase.Text.Trim();
            string salt = "";
            OurHash hashSystem = new OurHash();
            string Hash;
            Hash=hashSystem.ComputeHash(password, "MD5",null,ref salt);
           
            
            //SINH KEY RSA
            int keysize = Int32.Parse(txtKeySize.Text);
            OurRSA rsa = new OurRSA(keysize);

            //MA HOA PRIVATEKEY: DES

            //LUU TRU
            AccountDTO account = new AccountDTO();
            account.Address = txtAddress.Text.Trim();
            account.DayOfBirth = dtpDayOfBirth.Value;
            account.Email = txtEmail.Text;
            account.Hash = Hash;
            account.Name = txtName.Text;
            account.Phone = txtPhone.Text;
            account.PrivateKey = rsa.PrivateKeyXML;
            account.PublicKey = rsa.PublicKeyXML;
            account.Salt = salt;

            BUS.AccountBUS bus = new AccountBUS();

            if (bus.CreateNewAccount(account) != 0)
            {
                MessageBox.Show("Tài khoản đã được tạo!");
                Form f = this.FindForm();
                f.Close();
            }
            else
                MessageBox.Show("Không thể tạo tài khoản mới!");
        }

        private void txtConfirmPassword_Validated(object sender, EventArgs e)
        {
            string password = txtPassPhrase.Text;
            string passwordConfirm = txtConfirmPassword.Text;
            if (password.Trim()!=passwordConfirm.Trim())
            {
                MessageBox.Show("Xác nhận mật khẩu không đúng");
                txtConfirmPassword.Focus();
            }
        }

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtKeySize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtKeySize_Validated(object sender, EventArgs e)
        {
            int keysize = Int32.Parse(txtKeySize.Text);
            if (keysize < 512 || keysize > 1024 || keysize % 64 != 0)
            {
                MessageBox.Show("Yêu cầu nhập keysize theo qui định");
                txtKeySize.Clear();
                txtKeySize.Focus();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Form f = this.FindForm();
            f.Dispose();
        }
    }
}
